/*
 * commons is a based project implemented
 * Copyright (C) 2024 Jasmine
 */
package com.easy.core.utils;

import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * 密码编码工具类
 *
 * @author Jasmine
 * @since 1.0.0
 */

public class PasswordEncoderUtil {

    private static final PasswordEncoder passwordEncoder = ApplicationContextHolder.getBean(PasswordEncoder.class);

    private PasswordEncoderUtil() {}

    /**
     * 对原始密码进行编码。
     *
     * @param rawPassword 原始密码
     */
    public static String encode(CharSequence rawPassword) {
        return passwordEncoder.encode(rawPassword);
    }

    /**
     * 校验密码是否匹配
     *
     * @param rawPassword 对原始密码
     * @param encodedPassword 加密过的密码
     * @return true if the raw password, after encoding, matches the encoded password from storage
     */
    public static boolean matches(CharSequence rawPassword, String encodedPassword) {
        return passwordEncoder.matches(rawPassword, encodedPassword);
    }

}
